/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.resp.GetTrainByClassifyResponse;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.xy.biz.ts.domain.resp.GetLearnRecordResponse;
import com.xy.biz.ts.domain.req.GetLearnRecordRequest;
import com.xy.biz.ts.domain.req.ListLearnRecordRequest;
import com.xy.biz.ts.domain.req.SaveLearnRecordRequest;
import com.xy.biz.ts.domain.query.LearnRecordQuery;
import com.xy.biz.ts.domain.converter.LearnRecordConverter;
import com.xy.biz.ts.client.LearnRecordClient;
import com.xy.biz.ts.service.LearnRecordService;

/**
 *
 *
 * @author yzh
 * @date 2021-3-29
 */
@Api(tags = "学习记录")
@Slf4j
@RestController
@RequestMapping("/api/ts/learn_record")
public class LearnRecordController implements LearnRecordClient {

    @Autowired
    private LearnRecordService learnRecordService;

    @ApiOperation("新增学习记录")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveLearnRecord(@RequestBody SaveLearnRecordRequest request) {
        return learnRecordService.saveLearnRecord(LearnRecordConverter.toBO(request));
    }

    @Deprecated
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllLearnRecord(@RequestBody Collection<SaveLearnRecordRequest> collection) {
        learnRecordService.saveAllLearnRecord(LearnRecordConverter.toBO(collection));
        return Result.success();
    }

    @Deprecated
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateLearnRecord(@RequestBody SaveLearnRecordRequest request) {
        learnRecordService.updateLearnRecord(LearnRecordConverter.toBO(request));
        return Result.success();
    }

    @Deprecated
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteLearnRecord(@RequestBody GetLearnRecordRequest request) {
        learnRecordService.deleteLearnRecord(request.getLearnRecordId());
        return Result.success();
    }

    @Deprecated
    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetLearnRecordResponse> getLearnRecord(@RequestBody GetLearnRecordRequest request) {
        return Result.success(LearnRecordConverter.toResponse(learnRecordService.getLearnRecord(request.getLearnRecordId())));
    }

    @Deprecated
    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetLearnRecordResponse>> listLearnRecordById(@RequestBody Collection<Long> collection) {
        return Result.success(LearnRecordConverter.toResponse(learnRecordService.listLearnRecordById(collection)));
    }

    @Deprecated
    @PostMapping(value = "/list")
    @Override
    public Result<List<GetLearnRecordResponse>> listLearnRecord(@RequestBody ListLearnRecordRequest request) {
        return Result.success(learnRecordService.listLearnRecord(LearnRecordConverter.toQuery(request)));
    }

    @Deprecated
    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countLearnRecord(@RequestBody ListLearnRecordRequest request) {
        return Result.success(learnRecordService.countLearnRecord(LearnRecordConverter.toQuery(request)));
    }

    @ApiOperation("学习记录分页查询")
    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetLearnRecordResponse>> pageLearnRecord(@RequestBody ListLearnRecordRequest request) {
        LearnRecordQuery query = LearnRecordConverter.toQuery(request);
        if (request.getGetAllFlag() != null && request.getGetAllFlag()) {
            query.setRows(Integer.MAX_VALUE);
            query.setLessonType(1);
        } else {
            query.setLearnUserId(UserContextHolder.getUserId());
        }
        List<GetLearnRecordResponse> list = learnRecordService.listLearnRecord(query);
        int count = learnRecordService.countLearnRecord(query);
        return Result.success(new Page<>(list, count));
    }

}
